<?php
/* Project Babel
 *
 * Author: Xin, Liu (Livid)
 * File: /htdocs/core/AirmailCore.php
 * Usage: Airmail Class
 * Format: 1 tab indent(4 spaces), LF, UTF-8, no-BOM
 *
 * Subversion Keywords:
 *
 * $Id: AirmailCore.php 4 2007-03-03 18:43:59Z v2ex.livid $
 * $LastChangedDate: 2007-03-04 02:43:59 +0800 (星期日, 04 三月 2007) $
 * $LastChangedRevision: 4 $
 * $LastChangedBy: v2ex.livid $
 * $URL: http://project-babel.googlecode.com/svn/trunk/htdocs/core/AirmailCore.php $
 *
 * Copyright (C) 2006 Livid Liu <v2ex.livid@mac.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

if (@V2EX_BABEL != 1) {
	die('<strong>Project Babel</strong><br /><br />Made by <a href="http://www.v2ex.com/">V2EX</a> | software for internet');
}

/* S Airmail class */

/* type:

 0 => unknown
 1 => sign up
 2 => notify new reply for own topic
 
*/

class Airmail {
	public $body;

	public $headers;
	public $params;

	private $_mbox_unicode;

  private $_charset;
  private $_subject;
  private $_body;
  private $_to;

	public function __construct($receiver, $subject, $body, $db, $type = 0) {
    $this->_to = $receiver;$this->_subject = $subject;$this->_body = $body;
		$this->headers = array();
		require_once(BABEL_PREFIX . '/res/mbox_unicode.php');
		$this->_mbox_unicode = $_mbox_unicode;
		
		if(is_array($this->_mbox_unicode)) {
			foreach ($this->_mbox_unicode as $mbox) {
				if (preg_match(mbox_to_pattern($mbox), $receiver)) {
					$_flag_unicode = true;
				}
			}
		}

		if (!$_flag_unicode) {
			$this->headers['content-type'] = 'text/plain; charset=gb2312';
			$this->headers['subject'] = '=?GB2312?B?' . base64_encode(mb_convert_encoding($subject, 'GB2312', 'UTF-8')) . '?=';
			$this->body = mb_convert_encoding($body, 'GBK', 'UTF-8');
      $this->_charset = 'GB2312';
		} else {
			$this->headers['content-type'] = 'text/plain; charset=utf-8';
			$this->headers['subject'] = '=?UTF-8?B?' . base64_encode($subject) . '?=';
			$this->body = $body;
      $this->_charset = 'UTF-8';
		}

		$this->headers['from'] = BABEL_AM_FROM;
		$this->headers['to'] = $receiver;
		
		$this->params = array();
		$this->params["sendmail_path"] = '/usr/sbin/sendmail';
	}
	
	public function __destruct() {
	}
	
	public function vxSend() {
    if(SMTP_MAIL){
      $params = parse_url(MAIL_CFG);
      $_from = explode(" ", BABEL_AM_FROM);
      $_to = explode("@", $this->_to);
      $_type = 0;
      $subject = "=?".$this->_charset."?B?".base64_encode($this->_subject)."==?=";
      $body = base64_encode($this->_body);
      $headers[] = "To:=?".$this->_charset."?B?".base64_encode($_to[0])."?= <".$this->_to.">";
      $headers[] = "From:=?".$this->_charset."?B?".base64_encode($_from[0])."?= <$_from[1]>";
      $headers[] = "MIME-Version: MyBabel v1.0";
      $headers[] = "X-Mailer: MyBabel Mailer v1.0";
      $headers[] = "Subject:$subject";
      $headers[] = ($_type == 0) ? "Content-Type: text/plain; charset=".$this->_charset."; format=flowed" : "Content-Type: text/html; charset=".$this->_charset."; format=flowed";
      $headers[] = "Content-Transfer-Encoding: base64";
      $headers[] = "Content-Disposition: inline";

      $send_params['recipients']	= $this->_to;
      $send_params['headers']		= $headers;
      $send_params['from']		= $params['user'];
      $send_params['body']		= $body;
      require_once (BABEL_PREFIX.DS.'htdocs'.DS.'core'.DS.'MyBabelCore.php');
      
      $smtp = new smtp($params);
      return $smtp->connect() && $smtp->send($send_params);
    }else{
      $m =& Mail::factory('sendmail', $this->params);
      $m->send($this->headers['to'], $this->headers, $this->body);
    }
	}
}

/* E Airmail class */
?>
